import DataCenter from "./DataCenter";
import { ProductID, Topic_Control, Topic_Loop_Reply, Topic_Map, Topic_Master, Topic_Master_Reply, Topic_Strategy_Reply, Topic_Tunnel_Reply, Topic_UpdateIp, Topic_Update_LightMode, Topic_Debug, Topic_Property, Topic_Strategy, Topic_Tunnel, Topic_Loop, Topic_Async_Reply, Topic_Async, Topic_Update_Meter_Config, Topic_Update_Meter_Config_Reply } from "./LogicConstant";

export function formatterTopic(topic, serverId, gateId, productId) {
    let replace_topic = topic?.topic||topic||"";
    return replace_topic.replace("${serverId}",serverId ? serverId : "+").replace("${gateId}",gateId ? gateId : "+").replace("${productId}", productId ? productId : "+");
}

export function formatterSendTopic(topic, productId=ProductID.LOOP) {
    let replace_topic = topic?.topic||topic||"";
    return replace_topic.replace("${serverId}", DataCenter.serverId).replace("${gateId}", DataCenter.gateId).replace("${productId}", productId);
}

export function getNormalNodeLocalSubscriberTopics() {
    return [formatterTopic(Topic_Control, null, null, ProductID.LOOP), //物联网设备远程控制，不同设备control字段不同
        formatterTopic(Topic_Strategy_Reply, null, null, ProductID.LOOP), //同步调光策略信息
        formatterTopic(Topic_Tunnel_Reply, null, null, ProductID.LOOP), //同步洞内段/组/回路信息
        formatterTopic(Topic_Loop_Reply, null, null, ProductID.LOOP),//同步集群内柜子各个回路数据
        formatterTopic(Topic_Property, null, null),
        formatterTopic(Topic_Async_Reply, DataCenter.serverId, DataCenter.gateId),
        // formatterTopic(Topic_Update_LightMode, DataCenter.serverId, null, ProductID.LOOP),//更新调光模式
        ];
}

export function getMasterNodeLocalSubscriberTopics() {
    return [formatterTopic(Topic_Control, null, null, ProductID.LOOP),
        formatterTopic(Topic_Property, null, null),
        formatterTopic(Topic_Strategy, null, null, ProductID.LOOP), //同步调光策略信息
        formatterTopic(Topic_Tunnel, null, null, ProductID.LOOP), //同步洞内段/组/回路信息
        formatterTopic(Topic_Loop, null, null, ProductID.LOOP),//同步集群内柜子各个回路数据
        formatterTopic(Topic_Async, null, null),
            // formatterTopic(Topic_Update_LightMode, DataCenter.serverId, null, ProductID.LOOP),//更新调光模式
    ];
}

export function getMasterRemoteSubscriberTopics() {
    return [formatterTopic(Topic_Control, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP), //物联网设备远程控制，不同设备control字段不同
        formatterTopic(Topic_Strategy_Reply, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP), //同步调光策略信息
        formatterTopic(Topic_Tunnel_Reply, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP), //同步洞内段/组/回路信息
        formatterTopic(Topic_Loop_Reply, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP),//同步集群内柜子各个回路数据
        formatterTopic(Topic_UpdateIp, DataCenter.serverId, DataCenter.gateId),
        formatterTopic(Topic_Master_Reply, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP),
        formatterTopic(Topic_Update_LightMode, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP),//更新调光模式
        formatterTopic(Topic_Debug, DataCenter.serverId, DataCenter.gateId),    //  debug模式操作
        formatterTopic(Topic_Update_Meter_Config, DataCenter.serverId, DataCenter.gateId),    //  设置电表的查询配置
        ];
}

export function getNormalRemoteSubscriberTopics() {
    return [formatterTopic(Topic_Control, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP), //物联网设备远程控制，不同设备control字段不同
        formatterTopic(Topic_UpdateIp, DataCenter.serverId, DataCenter.gateId),
        formatterTopic(Topic_Master_Reply, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP),
        formatterTopic(Topic_Update_LightMode, DataCenter.serverId, DataCenter.gateId, ProductID.LOOP),//更新调光模式
        formatterTopic(Topic_Debug, DataCenter.serverId, DataCenter.gateId),    // debug模式操作
        formatterTopic(Topic_Update_Meter_Config, DataCenter.serverId, DataCenter.gateId),    //  设置电表的查询配置
        ];
}

export function getTopicMapping(topic) {
    try{
        let splits = (topic||"").split("/");
        if(splits && splits.length > 0) {
            return Topic_Map[splits[splits.length-1]];
        }
        return null;
    }catch(error) {
        return null;
    }
}